<template>
  <el-form
    :model="form"
    status-icon
    :rules="rules"
    ref="login-form"
    label-width="100px"
    class="login-container"
  >
    <h3 class="login-title">系统登录</h3>
    <el-form-item
      label="用户名"
      label-width="80px"
      prop="username"
      class="username"
    >
      <el-input
        type="input"
        v-model="form.username"
        auto-complete="off"
        placeholder="请输入账号"
      >
      </el-input>
    </el-form-item>
    <el-form-item label="密码" label-width="80px" prop="password">
      <el-input
        type="password"
        v-model="form.password"
        placeholder="请输入密码"
        clearable
      >
      </el-input>
    </el-form-item>
    <el-form-item class="login-submit">
      <el-button type="primary" @click="login" class="login-submit">
        登录
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import Mock from "mockjs";
import { mapMutations } from "vuex";
import { getMenu } from "@/api/data.js";
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            message: "用户名长度不能小于3位",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    ...mapMutations(["setToken", "clearMenu", "setMenu",'addMenu']),
    login() {
      this.$refs["login-form"].validate((valid) => {
        if (valid) {
          //   const token = Mock.Random.guid();
          //   this.setToken(token);
          //   this.$router.push({ name: "home" });
          getMenu(this.form).then((res) => {
              console.log(res)
            if (res.data.code == "20000") {
              this.clearMenu();
              this.setMenu(res.data.data.menu);
              this.setToken(res.data.data.token);
              this.addMenu(this.$router);
              this.$router.push({ name: "home" });
            } else {
              this.$message.error(res.data.message);
            }
          });
        } else {
          this.$message("请检查您的账号或密码", "确定");
          this.$refs["login-form"].resetFields();
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background-color: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
.login-title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.login-submit {
  margin: 10px auto 0px auto;
}
</style>
